package api

import (
	"gitee.com/tang_q/gin-vue3-admin-api/model/dto"
	"gitee.com/tang_q/gin-vue3-admin-api/model/product"
	"github.com/gin-gonic/gin"
)

type BrandApi struct {
	BaseApi
}

func NewBrandApi() BrandApi {
	return BrandApi{
		BaseApi: NewBaseApi(),
	}
}

func (brandApi BrandApi) GetBrandList(c *gin.Context) {
	brandApi.ctx = c
	req := dto.BrandQueryDTO{}
	if err := c.ShouldBind(&req); err != nil {
		brandApi.Fail(201, "参数错误", nil)
		return
	}
	brandList, total, err := product.GetBrandList(req)
	if err != nil {
		brandApi.Fail(201, "获取品牌列表失败", nil)
		return
	}
	brandApi.Success("获取品牌列表成功", gin.H{
		"brandList": brandList,
		"total":     total,
	})
}

func (brandApi BrandApi) GetRegions(c *gin.Context) {
	brandApi.ctx = c
	regions, err := product.GetBrandRegions()
	if err != nil {
		brandApi.Fail(201, "获取地区失败", nil)
		return
	}
	brandApi.Success("获取地区成功", regions)
}
func (b BrandApi) GetCategories(c *gin.Context) {
	b.ctx = c
	categories, err := product.GetBrandCategories()
	if err != nil {
		b.Fail(201, "获取分类失败:"+err.Error(), nil)
		return
	}
	b.Success("获取分类成功", categories)
}
func (b BrandApi) CreateRegOrCat(c *gin.Context) {
	b.ctx = c
	req := dto.AddRegOrCategoryDTO{}
	if err := c.ShouldBind(&req); err != nil {
		b.Fail(201, "参数错误", nil)
		return
	}
	if err := product.AddRegOrCategory(req); err != nil {
		b.Fail(201, "添加失败:"+err.Error(), nil)
		return
	}
	b.Success("添加成功", nil)
}
func (b BrandApi) CreateBrand(c *gin.Context) {
	b.ctx = c
	req := dto.CreateBrandDTO{}
	if err := c.ShouldBind(&req); err != nil {
		b.Fail(201, "参数错误", nil)
		return
	}
	if err := product.CreateBrand(req); err != nil {
		b.Fail(201, "添加失败:"+err.Error(), nil)
		return
	}
	b.Success("添加成功", nil)
}
func (b BrandApi) UpdateBrand(c *gin.Context) {
	b.ctx = c
	req := dto.UpdateBrandDTO{}
	if err := c.ShouldBind(&req); err != nil {
		b.Fail(201, "参数错误", nil)
	}
	if err := product.UpdateBrand(req); err != nil {
		b.Fail(201, "更新失败:"+err.Error(), nil)
		return
	}
	b.Success("更新成功", nil)
}
func (b BrandApi) DeleteBrand(c *gin.Context) {
	b.ctx = c
	id := c.Param("id")
	if id == "" {
		b.Fail(201, "参数错误", nil)
		return
	}
	if err := product.DeleteBrand(id); err != nil {
		b.Fail(201, "删除失败:"+err.Error(), nil)
		return
	}
	b.Success("删除成功", nil)
}
func (b BrandApi) DeleteBrandBatch(c *gin.Context) {
	b.ctx = c
	ids := dto.IdsDTO{}
	if err := c.ShouldBind(&ids); err != nil {
		b.Fail(201, "参数错误", nil)
		return
	}
	if err := product.DeleteBrandBatch(ids); err != nil {
		b.Fail(201, "删除失败:"+err.Error(), nil)
		return
	}
	b.Success("删除成功", nil)
}
